import React from 'react';
import PropTypes from 'prop-types';
import VoteHead from './VoteHead';
import VoteMain from './VoteMain';
import VoteFooter from './VoteFooter';
import ThemeContext from '../assets/Theme';

class Vote extends React.Component {
	/*
	 *  在祖先元素上注册后代需要使用的信息
	 */
	/* static childContextTypes = {
		title: PropTypes.string,
		supNum: PropTypes.number,
		oppNum: PropTypes.number,
		changeNum: PropTypes.func
	};
	getChildContext() {
		//=>getChildContext这个方法会在第一次属性和状态都初始化之后执行，和组件每一次重新渲染的时候执行（保证使用的属性和状态是最新的）
		return {
			title: this.props.title,
			supNum: this.state.supNum,
			oppNum: this.state.oppNum,
			changeNum: this.changeNum
		};
	} */

	state = {
		supNum: 0,
		oppNum: 0
	};
	changeNum = type => {
		if (type === 0) {
			this.setState({
				supNum: ++this.state.supNum
			});
			return;
		}
		this.setState({
			oppNum: ++this.state.oppNum
		});
	};
	render() {
		return <ThemeContext.Provider value={{
			title: this.props.title,
			supNum: this.state.supNum,
			oppNum: this.state.oppNum,
			changeNum: this.changeNum
		}}>
			<section>
				<VoteHead />
				<VoteMain />
				<VoteFooter />
			</section>
		</ThemeContext.Provider>;
	}
}
export default Vote;